package com.learn.mail.models;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public class Email extends Message {

    private String subject;
    private String sender;
    private List<String> cc;
    private List<String> bcc;

    public Email() {
    }

    /**
     * 邮件模板见 com.learn.message.server.template
     *
     * @param code
     * @param enterpriseId
     * @param to               收件人邮件地址
     * @param subject
     * @param templateParamMap
     * @return
     */
    public static Email newEmail(String code, String enterpriseId, String to, String subject,
                                 Map<String, Object> templateParamMap) {
        return Email.newEmail(code, null, enterpriseId, to, subject, templateParamMap, false);
    }

    public static Email newEmail(String code, String resourceId, String enterpriseId, String to, String subject, Map<String, Object> templateParamMap, boolean needRecord) {
        return newEmail(code, resourceId, enterpriseId, Lists.newArrayList(to.toUpperCase()), null,
                subject, templateParamMap, needRecord);

    }

    public static Email newEmail(String code, String enterpriseId, String to, List<String> cc, String subject, Map<String, Object> templateParamMap) {
        return newEmail(code, null, enterpriseId, Lists.newArrayList(to.toUpperCase()), cc, subject,
                templateParamMap, false);
    }

    public static Email newEmail(String code, String sender, List<String> to, String subject,
                                 List<Map<String, Object>> templateParamList, Map<String, List<Map<String, String>>> attachFiles) {

        Email email = new Email();
        email.setCode(code);
        email.setSender(sender);
        email.setReceiver(to);
        email.setTemplateParamList(templateParamList);
        email.setAttachFiles(attachFiles);
        return email;
    }

    public static Email newEmail(String code, String resourceId, String enterpriseId, List<String> to, List<String> cc, String subject,
                                 Map<String, Object> templateParamMap, boolean needRecord) {
        if (StringUtils.isBlank(code)) {
            throw new IllegalArgumentException("code is blank");
        }
        Email email = new Email();
        email.setCode(code);
//        email.setEnterpriseId(enterpriseId);
        email.setNeedRecord(needRecord);
//        if (StringUtils.isNotBlank(resourceId)) {
//            email.setId(MessageCategory.EMAIL + code + resourceId);
//        }
        email.setReceiver(to);
        email.setCc(cc);
        email.setSubject(subject);
        email.setTemplateParamMap(templateParamMap);
        return email;
    }

    /**
     * 邮件模板
     *
     * @param subject          主题
     * @param contentTemplate  内容模板
     * @param enterpriseId     企业ID
     * @param to               收件人邮件地址 - 集合
     * @param templateParamMap 模板参数 - 用于填充模板
     * @return Email
     */
    public static Email newTemplateEmail(String subject, String contentTemplate, String enterpriseId,
                                         List<String> to, Map<String, Object> templateParamMap) {
        return newTemplateEmail(subject, contentTemplate, enterpriseId, to, null, templateParamMap, null, null, false);
    }

    /**
     * @param subject          主题
     * @param contentTemplate  内容模板
     * @param enterpriseId     企业ID
     * @param to               收件人邮件地址-集合
     * @param cc               抄送收件人邮件地址 - 集合
     * @param templateParamMap 模板参数
     * @param attachFiles      附件,<附件类型,List<附件文件名,NAS文件路径>>
     * @param resourceId       业务资源ID 与needRecord及收件人，搭配使用,用于防止重复发送
     * @param needRecord       是否记录用于去重
     * @return Email
     */
    public static Email newTemplateEmail(String subject, String contentTemplate, String enterpriseId, List<String> to, List<String> cc,
                                         Map<String, Object> templateParamMap, Map<String, List<Map<String, String>>> attachFiles, String resourceId, boolean needRecord) {
        Email email = new Email();
//        if (null != resourceId) {
//            email.setResourceId(resourceId);
//        }
        email.setContent(contentTemplate);
//        email.setEnterpriseId(enterpriseId);
        email.setNeedRecord(needRecord);
        email.setCc(cc);
        email.setSubject(subject);
        email.setTemplateParamMap(templateParamMap);
        email.setAttachFiles(attachFiles);
        return email;
    }


    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public List<String> getCc() {
        return cc;
    }

    public void setCc(List<String> cc) {
        this.cc = cc;
    }

    public List<String> getBcc() {
        return bcc;
    }

    public void setBcc(List<String> bcc) {
        this.bcc = bcc;
    }
}
